package com.easy.web.controller.ord;

import com.alibaba.fastjson.JSONObject;
import com.easy.common.core.R;
import com.easy.common.utils.ServletUtils;
import com.easy.user.service.IHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.easy.common.utils.RedisConstants.LOGIN_USER_KEY;

/**
 * 浏览历史记录控制器
 *
 * @author RuoYu
 * @date 2023/02/05
 */
@RestController
@RequestMapping("/history")
@Api(tags = "浏览历史记录控制器")
public class HistoryController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IHistoryService historyService;

    /**
     * 获取浏览历史记录列表
     *
     * @return {@link R}
     */
    @GetMapping("list")
    @ApiOperation(value = "获取浏览历史记录列表")
    public R getFavoriteList(int page, int pageSize, String title) {
        String token = LOGIN_USER_KEY + ServletUtils.getRequest().getHeader("authorization");
        Long uid = Long.valueOf(stringRedisTemplate.opsForHash().get(token, "id") + "");

        JSONObject historyList = historyService.getHistoryList(page, pageSize, uid, title);

        return R.success(historyList);
    }

    @GetMapping("add")
    public R add(Long rid, Long uid) {
        historyService.addHistory(rid, uid);
        return R.success();
    }
}
